* Bernd Beber, Philip Roessler, and Alexandra Scacco
* Replication code for "Intergroup Violence and Political Attitudes: Evidence from a Dividing Sudan", Journal of Politics

* page numbers given below refer to page position, not printed page numbers
* run in Stata version 11.1
    version 11.1

* page 3, left column, and footnote 6:
    * comparison of riots to other kinds of violent non-war conflict using Social Conflict in Africa Database (SCAD) Version 3.0
    * data is available at <https://www.strausscenter.org/scad.html>
    use "SCAD 3.0 1990-2011.dta"
    * total number of riot events (both events that started as organized or spontaneous riots and those that escalated into riots)
    count if (etype==3 | etype==4 | escalation==3 | escalation==4) & sublocal==1
    * total number of events where issue is coded as "terrorism"
    count if (issue1==9 | issue2==9 | issue3==9) & sublocal==1
    * total number of anti- and extra-government violent events
    count if (etype==8 | etype==9 | escalation==8 | escalation==9) & sublocal==1
    * minimum number of total deaths in riot events
    sum ndeath if (etype==3 | etype==4 | escalation==3 | escalation==4) & sublocal==1 & ndeath>=0
    di `r(sum)'
    * minimum number of total deaths in events where issue is coded as "terrorism"
    sum ndeath if (issue1==9 | issue2==9 | issue3==9) & sublocal==1 & ndeath>=0
    di `r(sum)'
    * minimum number of total deaths in anti- and extra-government violent events
    sum ndeath if (etype==8 | etype==9 | escalation==8 | escalation==9) & sublocal==1 & ndeath>=0
    di `r(sum)'

* set up survey data
    use "Beber_Roessler_Scacco_JOP_Data"
    svyset pau [pweight=pweight], fpc(fpc_pau_strata) strata(strata) || _n, singleunit(certainty)
    * define set of AU indicators, with Haj Yousif as base category
    local aus au_abuanja au_boogaa au_shejara au_nasr
    * define set of indicators for non-Southern region of origin, with North-Central as base category
    local regions_r1 darfur_r1 nuba_r1 kordofan_r1 east_r1
    local regions_r2 darfur_r2 nuba_r2 kordofan_r2 east_r2
    * define set of individual-level control variables for each round of interviews
    local controls_r1 female_r1 age_r1 working_r1 selfemployed_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_log_r1 educ_father_log_r1
    local controls_r2 female_r2 age_r2 pr_working_r2 pr_selfemployed_r2 pr_wealth_factor_r2 pr_wealth_hh_hood_r2 pr_educ_log_r2 pr_educ_father_log_r2
    * define set of individual-level control variables when employment status is a covariate of interest
    * omit employment type and wealth measures because they are post-treatment
    local controls_work_r1 female_r1 age_r1 educ_log_r1 educ_father_log_r1

* page 1:
    * effective number of respondents
    count if pau!=.

* page 8, right column, and figure 2:
    * share of respondents supporting separation, by region of origin
    svy, subpop(if south_r1==0): prop d4_r1
    svy, subpop(if nuba_r1==1): prop d4_r1
    svy, subpop(if south_r1==1): prop d4_r1
    * share of respondents supporting citizenship for southerners, by region of origin
    svy, subpop(if south_r1==0): prop d9_r1
    svy, subpop(if nuba_r1==1): prop d9_r1
    svy, subpop(if south_r1==1): prop d9_r1

* page 9, left column:
    * share of northerners reporting fighting in neighborhood
    svy, subpop(if south_r1==0): prop riots_affected_r1

* page 9, table 1, with marginal effects reported in text on page 10, right column:
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 female_r1 age_r1 working_r1 selfemployed_r1 `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 female_r1 age_r1 wealth_factor_r1 wealth_hh_hood_r1 `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 female_r1 age_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 female_r1 age_r1 pau_paved_road_r1 pau_power_r1 pau_water_r1 pau_cell_r1 `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)

* page 10, footnote 27:
    * is effect going through reading Al Intibaha newspaper?
    * insignificant effect, with or without controlling for fighting in neighborhood
    svy, subpop(if south_r1==0): probit separation_r1 newspaper_intibaha_r1 `controls_r1' `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit separation_r1 newspaper_intibaha_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'

* page 10, footnote 29:
    * crosstab on effect of fighting in neighborhood
    * 10% of those without fighting support partition, compared to 17% of those with fighting
    svy, subpop(if south_r1==0): prop separation_r1, over(riots_affected_r1)

* page 10, footnote 30:
    * is there a problem with people moving away after the riot and only relatively sanguine people staying behind?
    * no, results hold without those who moved into their neighborhood within the last five years, i.e. since the riot
    svy, subpop(if south_r1==0 & !(a29_r1>=0 & a29_r1<=5)): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * excluding those who report having been displaced by riot (in round 2)
    svy, subpop(if south_r1==0 & riots_displaced_r2==0): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * only two non-southerners report having been displaced by the riot
    tab riots_displaced_r2 if south_r1==0
    * not that many people moved in any case
    * 18% of non-northerners have moved into neighborhood less than six years ago, 6% in 2005 and 2006
    svy, subpop(if south_r1==0): prop moved_r1
    svy, subpop(if south_r1==0): prop moved_riot_r1
    * comparable to 15% share of sample that had moved into neighborhood during the preceding six years
    gen tmp=1 if a29_r1>=6 & a29_r1<=11
    replace tmp=0 if tmp==. & a29_r1!=.
    svy, subpop(if south_r1==0): prop tmp
    drop tmp
    * 5% of non-northerners moved in 2003 and 2004
    gen tmp=1 if a29_r1==6 | a29_r1==7
    replace tmp=0 if tmp==. & a29_r1!=.
    svy, subpop(if south_r1==0): prop tmp
    drop tmp
    * are there differences across neighborhoods?
    * in fact, lower share of recent arrivals in Boogaa than Abu Anja, even though former saw less fighting
    svy, subpop(if south_r1==0 & au_abuanja==1): prop moved_r1
    svy, subpop(if south_r1==0 & au_boogaa==1): prop moved_r1
    svy, subpop(if south_r1==0 & au_hajyousif==1): prop moved_r1
    svy, subpop(if south_r1==0 & au_nasr==1): prop moved_r1
    svy, subpop(if south_r1==0 & au_shejara==1): prop moved_r1
    * no large-scale relocation into particular areas at time of riot
    svy, subpop(if south_r1==0 & au_abuanja==1): prop moved_riot_r1
    svy, subpop(if south_r1==0 & au_boogaa==1): prop moved_riot_r1
    svy, subpop(if south_r1==0 & au_hajyousif==1): prop moved_riot_r1
    svy, subpop(if south_r1==0 & au_nasr==1): prop moved_riot_r1
    svy, subpop(if south_r1==0 & au_shejara==1): prop moved_riot_r1

* page 11, table 2, panel A:
    * effect is robust to:
    * excluding nuba, who have fought alongside southerners
    svy, subpop(if south_r1==0 & nuba_r1==0): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * looking only at north-central respondents
    svy, subpop(if north_r1==1): probit separation_r1 riots_affected_r1 `controls_r1' `aus'
    * looking only at riverine Arabs (Danagla, Jaliyyin, and Shaygiyya)
    svy, subpop(if tribe_riverine_r1==1): probit separation_r1 riots_affected_r1 `controls_r1'
    svy, subpop(if tribe_riverine_r1==1): probit separation_r1 riots_affected_r1 `controls_r1' `aus'

* page 11, table 2, panel B:
    * inference about sample only
    probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus' if south_r1==0
    * ordered probit: interpret "don't know" as intermediate response for multinomial outcome
    svy, subpop(if south_r1==0): oprobit separation_m_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * multiple imputation for missing data on control variables
    preserve
        mi set wide
        mi register imputed `controls_r1'
        set seed 30042012
        mi impute logit female_r1 = age_r1 working_r1 selfemployed_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus', add(10) force
        mi impute regress age_r1 = female_r1 working_r1 selfemployed_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus', replace force
        mi impute ologit working_r1 = female_r1 age_r1 selfemployed_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus', replace force
        mi impute logit selfemployed_r1 = female_r1 age_r1 working_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus', replace force
        mi impute regress wealth_factor_r1 = female_r1 age_r1 working_r1 selfemployed_r1 wealth_hh_hood_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus', replace force
        mi impute ologit wealth_hh_hood_r1 = female_r1 age_r1 working_r1 selfemployed_r1 wealth_factor_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus', replace force
        mi impute regress educ_log_r1 = female_r1 age_r1 working_r1 selfemployed_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_father_log_r1 `regions_r1' `aus', replace force
        mi impute regress educ_father_log_r1 = female_r1 age_r1 working_r1 selfemployed_r1 wealth_factor_r1 wealth_hh_hood_r1 educ_log_r1 `regions_r1' `aus', replace force
        mi estimate: svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    restore

* page 11, table 2, panel C:
    * are residential patterns confounding the relationship between riot exposure and support for separation?
    * perhaps those who live close to southerners want them to leave and happened to be most affected by riot
    * we've already controlled for administrative unit, so have accounted for baseline location effect
    * but effect also robust to including respondent assessment of how many southerners live in neighborhood
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 pct_south_r1 `controls_r1' `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 pct_south_dk_r1 `controls_r1' `regions_r1' `aus'
    * we can also exclude those who say there are 10% or more Southerners in their neighborhood and get the same effect
    svy, subpop(if south_r1==0 & pct_south_r1<10): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * can also average across individual assessments within each PAU to get a more objective measure
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 pct_south_pau_r1 `controls_r1' `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 pct_south_dk_pau_r1 `controls_r1' `regions_r1' `aus'
    * use proportion of southerners in sample for each PAU as an alternative measure of proximity to southerners
    svy, subpop(if south_r1==0): probit separation_r1 riots_affected_r1 pct_south_pau_sample_r1 `controls_r1' `regions_r1' `aus'

* page 12, table 3, panel A:
    * how does effect of riot exposure compare to effect of wartime exposure to violence?
    * no effect for non-southerners in general
    svy, subpop(if south_r1==0): probit separation_r1 war_south_r1 `controls_r1' `regions_r1' `aus'
    * but estimate for north-central is similar to riot effect
    svy, subpop(if north_r1==1): probit separation_r1 war_south_r1 `controls_r1' `aus'
    * shouldn't war exposure be different because it affected northern soldiers as opposed to civilians?
    * actually, get the same effect if we exclude those who fought in the war against the south
    svy, subpop(if north_r1==1 & d36_r1==2): probit separation_r1 war_south_r1 `controls_r1' `aus'
    * also not about exposure to any violence, because darfur not significant
    svy, subpop(if north_r1==1): probit separation_r1 war_darfur_r1 `controls_r1' `regions_r1' `aus'
    * so did the riot just reactivate war memories?
    * no, riot exposure stays significant even if we exclude those who were affected by the war
    svy, subpop(if south_r1==0 & war_south_r1==0): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'

* page 13, footnote 33:
    * rerun model (4) in table 3, panel A, but include all non-southern respondents
    svy, subpop(if south_r1==0): probit separation_r1 war_darfur_r1 `controls_r1' `regions_r1' `aus'

* page 13, text and footnote 34, and figure 1:
    * see R code in file Beber_Roessler_Scacco_JOP_Kriging.R

* page 12, table 3, panel B:
    * not significant with individual-specific measure of riot exposure (injury, family hurt, displaced, separated, loss of job or property)
    svy, subpop(if south_r2==0): probit separation_r2 riots_direct_r2 `controls_r2' `regions_r2' `aus'
    * but significant when we include indirect exposure
    svy, subpop(if south_r2==0): probit separation_r2 riots_direct_indirect_r2 `controls_r2' `regions_r2' `aus'
    * direct exposure becomes significant if we only compare directly exposed individuals and those who were neither directly nor indirectly exposed
    svy, subpop(if south_r2==0 & (riots_direct_r2==1 | riots_direct_indirect_r2==0)): probit separation_r2 riots_direct_r2 `controls_r2' `regions_r2' `aus'

* page 14, table 4 and footnote 35:
    * effect of riot exposure on support for southerners retaining sudanese nationality
    svy, subpop(if south_r1==0): probit citizen_r1 riots_affected_r1 `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit citizen_r1 riots_affected_r1 female_r1 age_r1 working_r1 selfemployed_r1 `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit citizen_r1 riots_affected_r1 female_r1 age_r1 wealth_factor_r1 wealth_hh_hood_r1 `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit citizen_r1 riots_affected_r1 female_r1 age_r1 educ_log_r1 educ_father_log_r1 `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit citizen_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    svy, subpop(if south_r1==0): probit citizen_r1 riots_affected_r1 female_r1 age_r1 pau_paved_road_r1 pau_power_r1 pau_water_r1 pau_cell_r1 `regions_r1' `aus'

* page 15, table 5, with marginal effects reported in text on page 14, right column, and page 15, left column:
    * riot exposure makes people more pessimistic about restiveness of other regions
    svy, subpop(if south_r1==0): probit other_self_det_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if south_r1==0)
    * for north-central, diminished belief that partition will help peace in north sudan
    svy, subpop(if north_r1==1): probit peace_north_r1 riots_affected_r1 `controls_r1' `aus'
    margins, dydx(riots_affected) vce(unconditional) subpop(if north_r1==1)
    * for north-central, greater belief that partition will improve own physical security
    svy, subpop(if north_r1==1): probit security_pos_r1 riots_affected_r1 `controls_r1' `aus'

* online appendix, table A.1, referenced on page 8, footnote 22:
    * proportion of regions of origin in sample
    tab a15_r1 if a15_r1>0 & a15_r1<8
    * proportion of regions of origin in all of urban Khartoum
    * estimated population shares and figures for 2010
    prop a15_r1 if a15_r1>0 & a15_r1<8 [pweight=pweight_khartoum]
    sum pweight_khartoum
    local totalpre = `r(sum)'
    matrix p = e(b) * `totalpre'
    matrix list p

* online appendix, section A.3 and table A.2, referenced on page 4, left column:
    * does riot exposure have a large effect only for those who have nothing to lose economically?
    * no, we actually find an effect among both the employed and unemployed (a47==10, the modal category)
    svy, subpop(if south_r1==0 & a47_r1==10): probit separation_r1 riots_affected_r1 `controls_work_r1' `regions_r1' `aus'
    svy, subpop(if south_r1==0 & a47_r1!=10): probit separation_r1 riots_affected_r1 `controls_work_r1' `regions_r1' `aus'
    * strongest effect for those working at least part-time
    * don't include indicator kordofan_r1 because it perfectly predicts lack of support for separation in this subsample
    * get the same result if we include the indicator and let the relevant observations get dropped
    svy, subpop(if south_r1==0 & working_r1>0 & working_r1!=.): probit separation_r1 riots_affected_r1 `controls_work_r1' darfur_r1 nuba_r1 east_r1 `aus'
    svy, subpop(if south_r1==0 & working_r1>0 & working_r1!=.): probit separation_r1 riots_affected_r1 `controls_work_r1' `regions_r1' `aus'
    * also, we find that estimate holds for those who are pessimistic or neutral about effect of partition on economic status
    svy, subpop(if south_r1==0 & economic_r1==-1): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    svy, subpop(if south_r1==0 & economic_r1==0): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * doesn't hold for those optimistic, because they are more likely to be for partition with or without riot exposure
    svy, subpop(if south_r1==0 & economic_r1==1): probit separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus'
    * 32% of those optimistic about economic effects are for separation, 13% of those neutral, and 9% of those pessimistic
    svy, subpop(if south_r1==0): prop separation_r1, over(economic_r1)

* online appendix, section A.4 and figure A.3, referenced on page 12, footnote 32:
    * sensitivity analysis to show that main result is robust to certain range of unobservable confounders
    * output file Beber_Roessler_Scacco_JOP_Sensitivity.dta is provided with replication materials
    * use output to generate figure A.3 with R code in file Beber_Roessler_Scacco_JOP_Sensitivity.R
    * computationally intense, remove comment brackets to replicate
        /*
        * function to estimate probit regression for particular degree of correlation
        * specify coefficient to be saved in option `save' (same as endogenous regressor by default)
            cap program drop _sens
            program _sens, rclass
                syntax varlist [if] [in] [, save(varname) cy(real 0) cd(real 0)]
                marksample touse
                * specify varlist in the following order: outcome, potentially endogenous regressor, other covariates
                tokenize `varlist'
                local y `1'
                macro shift
                local d `1'
                macro shift
                local x `*'
                * which coefficient should be saved?
                if("`save'"=="") local save `d'
                * set up temporary variables
                tempvar z
                quietly gen `z' = `cy' * `y' + `cd' * `d' + invnorm(uniform())
                * hard-coded survey command!
                quietly svy, subpop(if south_r1==0): probit `y' `d' `z' `x' if `touse'
                return scalar coeff = _b[`save']
                return scalar sd = _se[`save']
                local t = _b[`save'] / _se[`save']
                return scalar p = 2 * ttail(e(df_r), abs(`t'))
                quietly corr `z' `d'
                return scalar rho_d = r(rho)
                quietly corr `z' `y'
                return scalar rho_y = r(rho)
            end
        * compute effect of violated exogeneity assumption
        * use grid options to set correlation
            cap program drop sens
            program sens, rclass
                syntax varlist [if] [in] [, save(varname) ygrid(numlist) dgrid(numlist)]
                marksample touse
                * set up variables to save results
                for any _coeff _sd _p _rho_d _rho_y, noheader: quietly gen X = .
                * set up temporary variables
                tempvar tosort cygrid cdgrid
                * prepare grid computation
                if "`ygrid'"=="" local ygrid "0(.1)1"
                if "`dgrid'"=="" local dgrid "0(.1)1"
                numlist "`ygrid'"
                local nygrid: word count `r(numlist)'
                numlist "`dgrid'"
                local ndgrid: word count `r(numlist)'
                local ngrid = `ndgrid' * `nygrid'
                * add additional observations if needed
                local nh = _N
                local nl = `ngrid' + 1
                cap set obs `ngrid'
                egen `cygrid' = fill(`ygrid' `ygrid')
                cap replace `cygrid' = . in `nl'/`nh'
                gen `tosort' = _n
                sort `cygrid' `tosort'
                egen `cdgrid' = fill(`dgrid' `dgrid')
                sort `tosort'
                cap replace `cdgrid' = . in `nl'/`nh'
                * compute regressions over grid
                forvalues i = 1/`ngrid' {
                    quietly {
                        local cy = `cygrid'[`i']
                        local cd = `cdgrid'[`i']
                        * varlist can be passed directly to _sens
                        cap _sens `varlist' if `touse', save(`save') cy(`cy') cd(`cd')
                        if _rc!=0 continue
                        replace _coeff = r(coeff) in `i'
                        replace _sd = r(sd) in `i'
                        replace _p = r(p) in `i'
                        replace _rho_d = r(rho_d) in `i'
                        replace _rho_y = r(rho_y) in `i'
                        * noisily di `i'
                    }
                }
            end
        * run sensitivity test
            preserve
                sens separation_r1 riots_affected_r1 `controls_r1' `regions_r1' `aus', save(riots_affected_r1) ygrid(-3(.1)3) dgrid(-3(.1)3)
                keep _coeff _sd _p _rho_d _rho_y
                save Beber_Roessler_Scacco_JOP_Sensitivity, replace
            restore
    * remove comment brackets to run
        */
